.set LOCALID_GROUDON, 1

TerraCave_End_MapScripts:: @ 823B0B0
	map_script MAP_SCRIPT_ON_RESUME, TerraCave_End_OnResume
	map_script MAP_SCRIPT_ON_TRANSITION, TerraCave_End_OnTransition
	.byte 0

TerraCave_End_OnResume: @ 823B0BB
	call_if_set FLAG_SYS_CTRL_OBJ_DELETE, TerraCave_End_EventScript_TryRemoveGroudon
	end

TerraCave_End_EventScript_TryRemoveGroudon:: @ 823B0C5
	specialvar VAR_RESULT, GetBattleOutcome
	compare VAR_RESULT, B_OUTCOME_CAUGHT
	goto_if_ne Common_EventScript_NopReturn
	removeobject LOCALID_GROUDON
	return

TerraCave_End_OnTransition: @ 823B0D9
	call_if_unset FLAG_DEFEATED_GROUDON, TerraCave_End_EventScript_ShowGroudon
	end

TerraCave_End_EventScript_ShowGroudon:: @ 823B0E3
	clearflag FLAG_HIDE_TERRA_CAVE_GROUDON
	setvar VAR_TEMP_1, 1
	return

TerraCave_End_EventScript_Groudon:: @ 823B0EC
	lockall
	applymovement OBJ_EVENT_ID_PLAYER, Common_Movement_FaceUp
	waitmovement 0
	applymovement LOCALID_GROUDON, TerraCave_End_Movement_GroudonApproach
	waitmovement 0
	waitse
	playmoncry SPECIES_GROUDON, 2
	delay 40
	waitmoncry
	setvar VAR_LAST_TALKED, LOCALID_GROUDON
	msgbox TerraCave_End_Text_GroudonSpeechBeforeBattle1, MSGBOX_DEFAULT
	playmoncry SPECIES_GROUDON, 2
	msgbox TerraCave_End_Text_GroudonSpeechBeforeBattle2, MSGBOX_DEFAULT
	@setwildbattle SPECIES_GROUDON, 70, ITEM_NONE
	setlegendarybattle SPECIES_GROUDON, 70, ITEM_RED_ORB, LEGENDARY_MON_GROUDON
	setflag FLAG_SYS_CTRL_OBJ_DELETE
	special BattleSetup_StartLegendaryBattle
	waitstate
	clearflag FLAG_SYS_CTRL_OBJ_DELETE
	setvar VAR_TEMP_1, 0
	specialvar VAR_RESULT, GetBattleOutcome
	@compare VAR_RESULT, B_OUTCOME_WON
	@goto_if_eq TerraCave_End_EventScript_DefeatedGroudon
	compare VAR_RESULT, B_OUTCOME_RAN
	goto_if_eq TerraCave_End_EventScript_RanFromGroudon
	compare VAR_RESULT, B_OUTCOME_PLAYER_TELEPORTED
	goto_if_eq TerraCave_End_EventScript_RanFromGroudon
	setvar VAR_SHOULD_END_ABNORMAL_WEATHER, 1
	setflag FLAG_DEFEATED_GROUDON
	msgbox TerraCave_End_Text_GroudonSpeechAfterBattle, MSGBOX_DEFAULT
	giveitem ITEM_GROUDON_VOUCHER, 1
	closemessage
	setvar VAR_0x8004, SPECIES_GROUDON
	goto Common_EventScript_DefeatLegendary
	@goto Common_EventScript_RemoveStaticPokemon
	@releaseall
	@end

@TerraCave_End_EventScript_DefeatedGroudon:: @ 823B155
@	setvar VAR_SHOULD_END_ABNORMAL_WEATHER, 1
@	setflag FLAG_DEFEATED_GROUDON
@	goto Common_EventScript_RemoveStaticPokemon
@	end

TerraCave_End_EventScript_RanFromGroudon:: @ 823B163
	setvar VAR_0x8004, SPECIES_GROUDON
	goto Common_EventScript_LegendaryFlewAway
	end

TerraCave_End_Movement_GroudonApproach: @ 823B16E
	init_affine_anim
	walk_down_start_affine
	delay_16
	delay_16
	walk_down_affine
	delay_16
	delay_16
	walk_down_affine
	delay_16
	delay_16
	clear_affine_anim
	step_end

TerraCave_End_Text_GroudonSpeechBeforeBattle1:
	.string "人……类……\n"
	.string "又……回到了初始的起点……吗……\p"
	.string "长久以来……只为寻求自然的力量……\n"
	.string "我与那家伙不断地争夺……\p"
	.string "所制造出的广阔陆地被海水埋没……\n"
	.string "无限的海洋被尘土填满……\p"
	.string "这样，直到终末……\n"
	.string "斗争也未停歇……\p"
	.string "在最后……\n"
	.string "我们无一例外陷入了沉睡……\p"
	.string "作为大地化身的我……\n"
	.string "残留在人类的记忆中………………$"
	
TerraCave_End_Text_GroudonSpeechBeforeBattle2:	
	.string "{COLOR RED}-----嘎啊啊啊！{COLOR DARK_GREY}{PAUSE 0x40}\p"
	.string "而现在……\n"
	.string "已经不需要了……\p"
	.string "我拥有了脚下的事物……\n"
	.string "因此屹立在此处。\p"
	.string "若是你……\n"
	.string "拥有征服大地的勇气……\p"
	.string "{COLOR RED}那{PAUSE 0x10}就{PAUSE 0x10}上{PAUSE 0x10}前{PAUSE 0x10}一{PAUSE 0x10}步{PAUSE 0x10}吧！$"

TerraCave_End_Text_GroudonSpeechAfterBattle:
	.string "你确实拥有着震动陆地的勇气……\n"
	.string "说不定……我和它难得没有意见相左。\p"
	.string "开启世界陆地化的协程吧……\n"
	.string "我会协助你的。\p"
	.string "毕竟……这是我为数不多……\n"
	.string "且忠于的事情。$"

